<?php

class TamusController extends \BaseController {

	/**
	 * Display a listing of tamus
	 *
	 * @return Response
	 */
	public function index()
	{
		$lokasis = array('' => 'Semua lokasi') + Lokasi::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$penduduks = array('' => 'Semua penduduk') + Penduduk::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$agamas = array('' => 'Semua agama') + Agama::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$jenispekerjaans = array('' => 'Semua pekerjaan') + JenisPekerjaan::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');

		return View::make('tamus.index')
						->with(compact('lokasis'))
						->with(compact('penduduks'))
						->with(compact('agamas'))
						->with(compact('jenispekerjaans'));
	}

	/**
	 * Show the form for creating a new tamus
	 *
	 * @return Response
	 */
	public function lokasi()
	{
		$lokasis = array('' => '- - Pilih Lokasi - -') + Lokasi::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		
		return View::make('tamus.lokasi')
								->with(compact('tamu'))
								->with(compact('lokasis'));
	}

	/**
	 * Store a newly created penduduk in storage.
	 *
	 * @return Response
	 */
	public function penduduk()
	{
		$lokasiId = Input::get('lokasi_input');
		$lokasiInput = Lokasi::where('id', $lokasiId)->orderBy('nama','ASC')->lists('nama', 'id');

		$penduduks = array('' => '- - Pilih Penduduk Penjamin - -') + Penduduk::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		
		return View::make('tamus.penduduk')
								->with(compact('tamu'))
								->with(compact('lokasiInput'))
								->with(compact('penduduks'));
	}

	/**
	 * Store a newly created penduduk in storage.
	 *
	 * @return Response
	 */
	public function create()
	{
		$lokasiId = Input::get('lokasi_input');
		$lokasiInput = Lokasi::where('id', $lokasiId)->orderBy('nama','ASC')->lists('nama', 'id');

		$pendudukId = Input::get('penduduk_penjamin');
		$pendudukInput = Penduduk::where('id', $pendudukId)->orderBy('nama','ASC')->lists('nama', 'id');

		$agamas = array('' => '- - Pilih Agama - -') + Agama::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$jenispekerjaans = array('' => '- - Pilih Jenis Pekerjaan - -') + JenisPekerjaan::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');

		return View::make('tamus.create')
								->with(compact('tamu'))
								->with(compact('lokasiInput'))
								->with(compact('pendudukInput'))
								->with(compact('agamas'))
								->with(compact('jenispekerjaans'));
	}

	/**
	 * Store a newly created tamus in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$validator = Validator::make($data = Input::all(), Tamu::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Tamu::create($data);

		return Redirect::route('tamus.index');
	}

	/**
	 * Display the specified tamus.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$tamus = Tamu::findOrFail($id);

		return View::make('tamus.show', compact('tamus'));
	}

	/**
	 * Show the form for editing the specified tamus.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$tamu = Tamu::find($id);

		$lokasis = array('' => '- - Pilih Lokasi - -') + Lokasi::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$penduduks = array('' => '- - Pilih Penduduk Penjamin - -') + Penduduk::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$agamas = array('' => '- - Pilih Agama - -') + Agama::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$jenispekerjaans = array('' => '- - Pilih Jenis Pekerjaan - -') + JenisPekerjaan::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');

		return View::make('tamus.edit')
								->with(compact('tamu'))
								->with(compact('lokasis'))
								->with(compact('penduduks'))
								->with(compact('agamas'))
								->with(compact('jenispekerjaans'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$validator = Validator::make($data = Input::all(), Tamu::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Tamu::create($data);

		$tamu = Tamu::findOrFail($id);
		$tamu->is_active = 0;
		$tamu->save();

		return Redirect::route('tamus.index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		// Tamu::destroy($id);

		$tamu = Tamu::findOrFail($id);
		$tamu->is_active = 0;
		$tamu->action = 3;
		$tamu->save();

		return Redirect::route('tamus.index');
	}

	public function filter()
	{
		$lokasis = array('' => 'Semua lokasi') + Lokasi::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$penduduks = array('' => 'Semua penduduk') + Penduduk::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$agamas = array('' => 'Semua agama') + Agama::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$jenispekerjaans = array('' => 'Semua pekerjaan') + JenisPekerjaan::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');

		return View::make('tamus.filter')
						->with(compact('lokasis'))
						->with(compact('penduduks'))
						->with(compact('agamas'))
						->with(compact('jenispekerjaans'));
	}

	public function datatableAllTamus()
	{
		$tamus = DB::table('tamus')    					    					
    					->join('jenis_pekerjaans', 'tamus.jenis_pekerjaan_id', '=', 'jenis_pekerjaans.id')
    					->join('lokasis', 'tamus.lokasi_id', '=', 'lokasis.id')
    					->join('penduduks', 'tamus.penduduk_id', '=', 'penduduks.id')
    					->join('agamas', 'tamus.agama_id', '=', 'agamas.id')
    					->select(array(
    						'tamus.id',
    						'tamus.no_ktp',
    						'tamus.nama',
    						DB::raw('YEAR(CURDATE())-YEAR(tamus.tanggal_lahir) AS umur'),
    						'tamus.jenis_kelamin',
    						'tamus.golongan_darah',
    						'agamas.nama AS agama',
    						'jenis_pekerjaans.nama AS jenis_pekerjaan',
    						'tamus.status_kawin',
    						'lokasis.nama AS lokasi',
    						'penduduks.nama AS penduduk'
    						));

  	if(Input::get('fl_no_ktp') != "") {
  		$tamus = $tamus->where('tamus.no_ktp', 'LIKE', '%'.Input::get('fl_no_ktp').'%');
  	}
		if(Input::get('fl_nama') != "") {
  		$tamus = $tamus->where('tamus.nama', 'LIKE', '%'.Input::get('fl_nama').'%');
  	}
  	if(Input::get('fl_jk') != "") {
  		$tamus = $tamus->where('tamus.jenis_kelamin', '=', Input::get('fl_jk'));
  	}
  	if(Input::get('fl_gol') != "") {
  		$tamus = $tamus->where('tamus.golongan_darah', '=', Input::get('fl_gol'));
  	}
  	if(Input::get('fl_agama') != "") {
  		$tamus = $tamus->where('tamus.agama_id', '=', Input::get('fl_agama'));
  	}
  	if(Input::get('fl_pekerjaan') != "") {
  		$tamus = $tamus->where('tamus.pekerjaan_id', '=', Input::get('fl_pekerjaan'));
  	}
  	if(Input::get('fl_kawin') != "") {
  		$tamus = $tamus->where('tamus.status_kawin', '=', Input::get('fl_kawin'));
  	}
  	if(Input::get('fl_lokasi') != "") {
  		$tamus = $tamus->where('lokasis.nama', 'LIKE', '%'.Input::get('fl_lokasi').'%');
  	}
  	if(Input::get('fl_penduduk') != "") {
  		$tamus = $tamus->where('penduduks.nama', 'LIKE', '%'.Input::get('fl_penduduk').'%');
  	}
  	if(Input::get('fl_umur_awal') != "") {
  		$tamus = $tamus->where(DB::raw('YEAR(CURDATE())-YEAR(tamus.tanggal_lahir)'), '>=', Input::get('fl_umur_awal'));
  	}
  	if(Input::get('fl_umur_akhir') != "") {
  		$tamus = $tamus->where(DB::raw('YEAR(CURDATE())-YEAR(tamus.tanggal_lahir)'), '<=', Input::get('fl_umur_akhir'));
  	}
  	$tamus = $tamus->where('tamus.is_active', '=', '1')->orderBy('tamus.nama','ASC');

		return Datatables::of($tamus)
		->add_column('operations', '<div class="btn-group">
										<a href="{{ URL::route(\'tamus.show\', $id) }}" class="btn btn-default btn-tooltip" data-toggle="tooltip" data-placement="top" data-original-title="Detail"><i class="fa fa-list text-success"></i></a>
										<a href="{{ URL::route(\'tamus.edit\', $id) }}" class="btn btn-default btn-tooltip" data-toggle="tooltip" data-placement="top" data-original-title="Edit"><i class="fa fa-pencil text-warning"></i></a>
										<a href="{{ URL::route(\'tamus.destroy\', $id) }}" class="btn btn-default btn-delete" data-toggle="tooltip" data-placement="top" data-original-title="Hapus"><i class="fa fa-times text-danger"></i></a>
									</div>')
		->make();
	}

}